package com.statistics.export;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WordExportDemo {

    public static void main(String[] args) throws IOException {
//        ApiDemo apiDemo = new ApiDemo();
//        System.out.println(apiDemo);
//        //获得Apache POI增强类NiceXWPFDocument
//        NiceXWPFDocument doc =  new NiceXWPFDocument();

        XWPFDocument doc = new XWPFDocument();

        XWPFParagraph actTheme = doc.createParagraph();//设置活动主题
        actTheme.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun runText1 = actTheme.createRun();
        runText1.setText("接口名称");
        runText1.setFontSize(18);


        XWPFParagraph actTheme1 = doc.createParagraph();//设置活动主题
        actTheme1.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun runText2 = actTheme1.createRun();
        runText2.setText("基本信息");
        runText2.setFontSize(15);

        XWPFTable table1 = doc.createTable();

        //插入表格

        List<String> list = new ArrayList<>();
        list.add("名称");
        list.add("类型");
        list.add("必填");
        list.add("说明");
        XWPFTableRow row = table1.getRow(0);
        for (int i = 0; i <= 3; i++) {
            switch (i) {
                case 0:
                    XWPFTableCell cell = row.getCell(i);
                    cell.setWidth("40");
                    cell.setWidthType(TableWidthType.NIL);
                    cell.setColor("8e909c");
                    cell.setText(list.get(i));
                    break;
                case 1:
                    XWPFTableCell creCell = row.addNewTableCell();
                    creCell.setWidth("80");
                    creCell.setWidthType(TableWidthType.NIL);
                    creCell.setColor("8e909c");
                    creCell.setText(list.get(i));
                    break;
                case 2:
                    XWPFTableCell creCell1 = row.addNewTableCell();
                    creCell1.setWidth("120");
                    creCell1.setWidthType(TableWidthType.NIL);
                    creCell1.setColor("8e909c");
                    creCell1.setText(list.get(i));
                case 3:
                    XWPFTableCell creCell2 = row.addNewTableCell();
                    creCell2.setWidth("180");
                    creCell2.setWidthType(TableWidthType.NIL);
                    creCell2.setColor("8e909c");
                    creCell2.setText(list.get(i));
                    break;
                default:
                    break;
            }

        setTableLocation(table1, "left");
        setCellLocation(table1, "CENTER", "center");
    }
//            doc.removeBodyElement(doc.getPosOfParagraph(xwpfParagraph));


//
//        XWPFParagraph actType = doc.createParagraph();//设置活动类型
//        XWPFRun runText2=actType.createRun();
//        runText2.setText("2、活动类型：系统发劵类型");
//        runText2.setFontSize(13);
//
//        XWPFParagraph actDate = doc.createParagraph();//设置活动日期
//        XWPFRun actDaterun=actDate.createRun();
//        actDaterun.setText("3、活动日期：2015-06-08至2015-06-10");
//        actDaterun.setFontSize(13);
//
//        XWPFParagraph actText = doc.createParagraph();//设置活动内容
//        XWPFRun runText3=actText.createRun();
//        runText3.setFontSize(13);
//        runText3.setText("4、活动内容：哈哈哈士大夫士大夫立刻绝对是方路即可大水井坊路可绝对是弗兰克家第三方立刻几点睡了罚款绝对是路客服绝对是路客服绝对是路客服几点睡了罚款家第三方立刻几点睡了罚款记录可定时 ");
//
//        XWPFParagraph actRemark = doc.createParagraph();//设置活动备注
//        XWPFRun runText4=actRemark.createRun();
//        runText4.setText("5、活动备注： ");
//        runText4.setFontSize(13);
//        runText4.setBold(true);
//        XWPFRun runText5=actRemark.createRun();
//        runText5.setText("我是活动备注哦........................ ");
//        runText5.setFontSize(12);
//
//        XWPFParagraph actRule = doc.createParagraph();//设置活动备注
//        XWPFRun rule=actRule.createRun();
//        rule.setText("6、活动规则： ");
//        rule.setFontSize(13);
//        rule.setBold(true);
//
//        XWPFTable table=actRule.getDocument().createTable(2,2);//创建表格
//        table.setWidth(500);
//        table.setCellMargins(20, 20, 20, 20);
//        System.out.println(table.getWidth());
//
//        //表格属性
//        CTTblPr tablePr = table.getCTTbl().addNewTblPr();
//        //表格宽度
//        CTTblWidth width = tablePr.addNewTblW();
//        width.setW(BigInteger.valueOf(8000));
//
//        List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
//        tableCells.get(0).setText("第一行第一列的数据:规则类型名称");
//        tableCells.get(1).setText("第一行第二列的数据:规则描述");
//
//        List<XWPFTableCell> tableCellsq = table.getRow(1).getTableCells();
//        tableCellsq.get(0).setText("第二行第一列的数据:A发劵规则");
//        tableCellsq.get(1).setText("第二行第二列的数据:A发劵规则针对5星级用户");
//
//        XWPFParagraph text9 = doc.createParagraph();
//        XWPFRun runText9=text9.createRun();
//        runText9.addBreak(BreakType.TEXT_WRAPPING);//换行
//        runText9.setBold(true);
//        runText9.setText("7、请现在你喜欢运动？");
//        runText9.setFontSize(13);
//
//        XWPFParagraph text10 = doc.createParagraph();
//        XWPFRun runText10=text10.createRun();
//        runText10.setFontSize(12);
//        runText10.setText("□a.排球       ");
//        runText10.setText("☑b.羽毛球");
//
//
//        XWPFParagraph text3 = doc.createParagraph();
//        XWPFRun runText7=text3.createRun();
//        runText7.setText("负责人：zhangsan");
//        runText7.setFontSize(13);
//
//        XWPFParagraph text8 = doc.createParagraph();
//        XWPFRun runText8=text8.createRun();
//        runText8.setText("负责人电话：12345678921");
//        runText8.setFontSize(13);


        FileOutputStream out = new FileOutputStream("D:\\下载test.docx");

        doc.write(out);
        System.out.println(1);
        out.close();


    }

    /**
     * 设置单元格水平位置和垂直位置
     *
     * @param xwpfTable
     * @param verticalLoction    单元格中内容垂直上TOP，下BOTTOM，居中CENTER，BOTH两端对齐
     * @param horizontalLocation 单元格中内容水平居中center,left居左，right居右，both两端对齐
     */
    public static void setCellLocation(XWPFTable xwpfTable, String verticalLoction, String horizontalLocation) {
        List<XWPFTableRow> rows = xwpfTable.getRows();
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> cells = row.getTableCells();
            for (XWPFTableCell cell : cells) {
                CTTc cttc = cell.getCTTc();
                CTP ctp = cttc.getPList().get(0);
                CTPPr ctppr = ctp.getPPr();
                if (ctppr == null) {
                    ctppr = ctp.addNewPPr();
                }
                CTJc ctjc = ctppr.getJc();
                if (ctjc == null) {
                    ctjc = ctppr.addNewJc();
                }
                ctjc.setVal(STJc.Enum.forString(horizontalLocation)); //水平居中
                cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.valueOf(verticalLoction));//垂直居中
            }
        }
    }


    /**
     * 设置表格位置
     *
     * @param xwpfTable
     * @param location  整个表格居中center,left居左，right居右，both两端对齐
     */
    public static void setTableLocation(XWPFTable xwpfTable, String location) {
        CTTbl cttbl = xwpfTable.getCTTbl();
        CTTblPr tblpr = cttbl.getTblPr() == null ? cttbl.addNewTblPr() : cttbl.getTblPr();
        CTJc cTJc = tblpr.addNewJc();
        cTJc.setVal(STJc.Enum.forString(location));
    }
}
